package com.infore.airsupervise.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.infore.airsupervise.R;
import com.infore.airsupervise.app.AppInfo;
import com.inqbarna.tablefixheaders.adapters.BaseTableAdapter;

/**
 * 点位分析表格适配器
 * <p>
 * 作者: dingxinlin
 * 创建日期: 2017/4/7 14:12
 */

public class PointAnalyzeTableAdapter extends BaseTableAdapter {

    private Context context;
    private LayoutInflater layoutInflater;

    private String[] topTitle;
    private String[] leftTitle;
    private String[][] data;

    private int headColor;
    private int rowColor;

    private int leftTitleWidth = 80;
    private int width = 120;
    private int height = 40;

    /**
     * Constructor
     *
     * @param context The current context.
     */
    public PointAnalyzeTableAdapter(Context context, String[] topTitle, String[] leftTitle,  String[][] data) {
        this.context = context;
        this.topTitle = topTitle;
        this.leftTitle = leftTitle;
        this.data = data;
        layoutInflater = LayoutInflater.from(context);

        headColor = context.getResources().getColor(R.color.colorAccent);
        rowColor = context.getResources().getColor(R.color.tab_row);
    }

    public void updata(String[] topTitle, String[] leftTitle,  String[][] data){
        this.topTitle = topTitle;
        this.leftTitle = leftTitle;
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getRowCount() {

        return data==null ?0 :data.length;
    }

    @Override
    public int getColumnCount() {
        return data==null ?0:data[0].length;
    }

    @Override
    public View getView(int row, int column, View convertView, ViewGroup parent) {
        convertView = layoutInflater.inflate(R.layout.item_table, parent, false);

        TextView textView = (TextView) convertView.findViewById(android.R.id.text1);

        switch (getItemViewType(row, column)) {
            case 0:
                textView.setText("\\");
                textView.setTextColor(Color.WHITE);
                convertView.setBackgroundColor(headColor);
                break;

            case 1:
                textView.setText(topTitle[column]);
                textView.setTextColor(Color.WHITE);
                convertView.setBackgroundColor(headColor);
                break;

            case 2:
                textView.setText(leftTitle[row]);
                convertView.setBackgroundColor(
                        row % 2 == 0 ? Color.WHITE : rowColor);
                break;

            case 3:
                textView.setText(data[row][column]);
                convertView.setBackgroundColor(
                        row % 2 == 0 ? Color.WHITE : rowColor);
                break;
        }

        return convertView;
    }

    @Override
    public int getWidth(int column) {
        if (column == -1) {
            return Math.round(leftTitleWidth * AppInfo.DENSITY);
        } else {
            return Math.round(width * AppInfo.DENSITY);
        }
    }

    @Override
    public int getHeight(int row) {
        return Math.round(height * AppInfo.DENSITY);
    }

    @Override
    public int getItemViewType(int row, int column) {
        if (row == -1 && column == -1) {
            return 0;
        } else if (row < 0) {
            return 1;
        } else if (column < 0){
            return 2;
        } else {
            return 3;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

}
